package com.pattern.structure.composite;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yangguanzan
 * @date 7/3/22
 * @description
 */
public class BranchSafe extends CorpBase {

    private List<CorpBase>  children;

    public BranchSafe(String name) {
        super(name);
        children = new ArrayList<>();
    }

    public CorpBase getChild(int i) {
        return children.get(i);
    }

    public void allOperatetion(){
        super.operation();
        for(CorpBase base:children){
            if(base instanceof BranchSafe){
                ((BranchSafe) base).allOperatetion();
            }else{
                base.operation();
            }
        }
    }

    public void add(CorpBase node) {
        node.setParent(this);
        children.add(node);
    }

    public void remove(CorpBase node) {
        children.remove(node);
    }
}
